# -*- coding: utf-8 -*-
# transform data to svdfeature

fin1 = open("../data/invited_info_train_purified.txt")
fin2 = open("../data/validate_nolabel.txt")
fin_user = open("../data/user_info.txt")
fin_ques = open("../data/question_info.txt")
fout1 = open("/home/moore/myWorkplace/svdfeature-1.2.2/demo/binaryClassification/ua.base.example.multi", "w")
fout2 = open("/home/moore/myWorkplace/svdfeature-1.2.2/demo/binaryClassification/ua.test.example.multi", "w")

dic_user = {}
dic_ques = {}
dic_u = {}
dic_q = {}

for line in fin_user:
	line = line.strip()
	line_list = line.split("\t")
	#0:user 1:labels 2:word 3:char
	if line_list[0] not in dic_user:
		labels = line_list[1].split("/")
		dic_user[line_list[0]] = labels
fin_user.close()
	
for line in fin_ques:
	line = line.strip()
	line_list = line.split("\t")
	#0:ques 1:label 2:word 3:char
	if line_list[0] not in dic_ques:
		labels = line_list[1].split("/")
		dic_ques[line_list[0]] = labels
fin_ques.close()

print dic_user["e6a2ecac7f90d426103de95ba7f6d2b0"] #no1	
print dic_ques["1c525c9d44135bdc40ddd4b8d63738d8"] #no3

for line in fin1:
	line = line.strip()
	line_list = line.split("\t")
	#0:question 1:user 2:label
	out_line = ""
	out_line = out_line + line_list[2] + " 0 "
	user_feature_num = len(dic_user[line_list[1]]) + 1
	ques_feature_num = len(dic_ques[line_list[0]]) + 1
	out_line = out_line + str(user_feature_num) + " " + str(ques_feature_num) + " "
	if line_list[1] not in dic_u:
		dic_u[line_list[1]] = str(len(dic_u))
	out_line = out_line + str(int(dic_u[line_list[1]])+143) + ":1 "
	for index in dic_user[line_list[1]]:
		if index == "150":
			print line_list[1]
		out_line = out_line + index + ":1 "

	if line_list[0] not in dic_q: 
		dic_q[line_list[0]] = str(len(dic_q))
	out_line = out_line + str(int(dic_q[line_list[0]])+20) + ":1"
	for index in dic_ques[line_list[0]]:
		out_line = out_line + " " + index + ":1"
	out_line = out_line + "\n"
	fout1.write(out_line)
fin1.close()

for line in fin2:
	line = line.strip()
	line_list = line.split(",")
	#0:question 1:user
	out_line = "0 0 "
	user_feature_num = len(dic_user[line_list[1]]) + 1
	ques_feature_num = len(dic_ques[line_list[0]]) + 1
	out_line = out_line + str(user_feature_num) + " " + str(ques_feature_num) + " "

	if line_list[1] not in dic_u:
		dic_u[line_list[1]] = str(len(dic_u))
	out_line = out_line + str(int(dic_u[line_list[1]])+143) + ":1 "
	for index in dic_user[line_list[1]]:
		out_line = out_line + index + ":1 "

	if line_list[0] not in dic_q:
		dic_q[line_list[0]] = str(len(dic_q))
	out_line = out_line + str(int(dic_q[line_list[0]])+20) + ":1"
	for index in dic_ques[line_list[0]]:
		out_line = out_line + " " + index + ":1"
	out_line = out_line + "\n"
	fout2.write(out_line)
fout2.close()
print "ques num: ", len(dic_q)
print "user num: ", len(dic_u)